import {Body, Controller, Get, Post, UploadedFile, UploadedFiles, UseInterceptors} from '@nestjs/common';
import { AppService } from './app.service';
import {FileFieldsInterceptor, FileInterceptor, FilesInterceptor} from "@nestjs/platform-express";
import {storage} from "./uploadStorage";

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }

  @Post('aaa')
  @UseInterceptors(FileInterceptor('file', {
    storage: storage
  }))
  uploadFile(@UploadedFile() file: Express.Multer.File, @Body() body): boolean {
    console.log(file)
    console.log(body)
    return true
  }

  @Post('bbb')
  @UseInterceptors(FilesInterceptor('files', 2 ,{
    dest: 'uploads'
  }))
  uploadFiles(@UploadedFiles() files: Array<Express.Multer.File>, @Body() body) {
    console.log(files)
    console.log(body)
    return 'upload success'
  }

  @Post('ccc')
  @UseInterceptors(FileFieldsInterceptor([
    { name: 'aFile', maxCount: 2 },
    { name: 'bFile', maxCount: 3 },
  ], {
    dest: 'uploads'
  }))
  uploadFileOfField(@UploadedFiles() files: { aFile?: Express.Multer.File[], b?: Express.Multer.File[] }, @Body() body) {
    console.log(files)
    console.log(body)
  }
}
